package com.house.customer.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.house.customer.entity.Customer;

/**
 *  DTO  封装查询条件 数据传输对象
 */
public class CustomerDTO {

    private Long id;          //主键（买/租）
	
	private String name;      //名字
	
	private String state;     //状态
	private String sex;       //性别
	
	private int phone;        //手机号
	private String email;     //邮箱号
	private Date checkInTime; //入住时间
	private String community; //需求小区
	private int area;         //需求面积/平方米
	private String houseType; //需求价格/元
	private int price;        //需求价格
	private String recorder;  //记录员
	private Date recordTime;  //录入时间
	

	public Long getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public String getState() {
		return state;
	}
	public String getSex() {
		return sex;
	}
	public int getPhone() {
		return phone;
	}
	public String getEmail() {
		return email;
	}
	public Date getCheckInTime() {
		return checkInTime;
	}
	public String getCommunity() {
		return community;
	}
	public int getArea() {
		return area;
	}
	public String getHouseType() {
		return houseType;
	}
	public int getPrice() {
		return price;
	}
	public String getRecorder() {
		return recorder;
	}
	public Date getRecordTime() {
		return recordTime;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setState(String state) {
		this.state = state;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void setPhone(int phone) {
		this.phone = phone;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setCheckInTime(Date checkInTime) {
		this.checkInTime = checkInTime;
	}
	public void setCommunity(String community) {
		this.community = community;
	}
	public void setArea(int area) {
		this.area = area;
	}
	public void setHouseType(String houseType) {
		this.houseType = houseType;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public void setRecorder(String recorder) {
		this.recorder = recorder;
	}
	public void setRecordTime(Date recordTime) {
		this.recordTime = recordTime;
	}
	
	

	//提供static的工具方法：根据当前 userQueryDTO 对象来组装动态查询条件
	public static Specification<Customer> getSpecification(CustomerDTO customerDTO)
	{
		Specification<Customer> spec = new Specification<Customer>() {
			public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getId())) {
					 Predicate  p1 =  cb.like(root.get("id").as(String.class),"%"+ customerDTO.getId() + "%");
					 list.add(p1);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getName())) {
					 Predicate  p2 =  cb.like(root.get("name").as(String.class),"%"+ customerDTO.getName() + "%");
					 list.add(p2);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getState())) {
					 Predicate  p3 =  cb.like(root.get("state").as(String.class),"%"+ customerDTO.getState() + "%");
					 list.add(p3);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getSex())) {
					 Predicate  p4 =  cb.like(root.get("sex").as(String.class),"%"+ customerDTO.getSex() + "%");
					 list.add(p4);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getPhone())) {
					 Predicate  p5 =  cb.like(root.get("phone").as(String.class),"%"+ customerDTO.getPhone() + "%");
					 list.add(p5);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getEmail())) {
					 Predicate  p6 =  cb.like(root.get("email").as(String.class),"%"+ customerDTO.getEmail() + "%");
					 list.add(p6);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getCheckInTime())) {
					 Predicate  p7 =  cb.like(root.get("checkInTime").as(String.class),"%"+ customerDTO.getCheckInTime() + "%");
					 list.add(p7);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getCommunity())) {
					 Predicate  p8 =  cb.like(root.get("community").as(String.class),"%"+ customerDTO.getCommunity() + "%");
					 list.add(p8);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getArea())) {
					 Predicate  p9 =  cb.like(root.get("area").as(String.class),"%"+ customerDTO.getArea() + "%");
					 list.add(p9);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getHouseType())) {
					 Predicate  p10 =  cb.like(root.get("houseType").as(String.class),"%"+ customerDTO.getHouseType() + "%");
					 list.add(p10);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getPrice())) {
					 Predicate  p11 =  cb.like(root.get("price").as(String.class),"%"+ customerDTO.getPrice() + "%");
					 list.add(p11);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getRecorder())) {
					 Predicate  p12 =  cb.like(root.get("recorder").as(String.class),"%"+ customerDTO.getRecorder() + "%");
					 list.add(p12);
				 }
				 if(null != customerDTO && !StringUtils.isEmpty(customerDTO.getRecordTime())) {
					 Predicate  p13 =  cb.like(root.get("recordTime").as(String.class),"%"+ customerDTO.getRecordTime() + "%");
					 list.add(p13);
				 }
						 			 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
	
	
}
